<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="轮播图列表"
      row-key="id"
      :tool-button="false"
      :indent="20"
      :columns="columns"
      :data-callback="dataCallback"
      :request-api="getAdvertList"
    >
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增', initData)"> 新增轮播图 </el-button>
      </template>
      <template #imageUrl="scope">
        <div class="demo-image__preview">
          <el-image
            style="width: 300px; height: 180px"
            :src="scope.row.imageUrl"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="previewSrcList"
            :initial-index="4"
            :preview-teleported="true"
            fit="cover"
          />
        </div>
      </template>
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="EditPen" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteCategory(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <AdvertDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="slideImg">
import { ref, reactive } from "vue";
import { Online } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { advertStatus } from "@/utils/serviceDict";
import { getAdvertList, editAdvert, deleteAdvert, createAdvert } from "@/api/modules/online";
import AdvertDialog from "@/views/online/components/AdvertDialog.vue";
import { CirclePlus, EditPen, Delete } from "@element-plus/icons-vue";

const proTable = ref<ProTableInstance>();
const previewSrcList = ref([]);
// 新建默认数据
const initData = reactive({
  id: 0,
  status: 1
});

const dataCallback = (data: any) => {
  previewSrcList.value = data.list.map(im => im.imageUrl);
  return {
    list: data.list,
    total: data.total,
    pageNum: data.pageNum,
    pageSize: data.pageSize
  };
};

const columns: ColumnProps<Online.ResAdvertList>[] = [
  { type: "index", label: "序号", width: 80 },
  { prop: "imageUrl", label: "轮播图", width: 330 },
  { prop: "name", label: "名称", minWidth: 140, align: "left" },
  {
    prop: "status",
    label: "是否启用",
    enum: advertStatus,
    minWidth: 120
  },
  { prop: "operation", label: "操作", width: 220 }
];

const deleteCategory = async (params: Online.ResAdvertList) => {
  await useHandleData(deleteAdvert, { id: params.id }, `删除【${params.name}】轮播图`);
  proTable.value?.getTableList();
};

const dialogRef = ref<InstanceType<typeof MemberGradeDialog> | null>(null);
const openDialog = (title: string, row: Partial<Online.ResAdvertList> = {}) => {
  const params = {
    title,
    row: { ...row },
    isView: title === "查看",
    api: title === "新增" ? createAdvert : title === "编辑" ? editAdvert : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
